package com.dhms.uikit

import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.cardview.widget.CardView
import androidx.core.content.ContextCompat
import androidx.databinding.BindingAdapter


@BindingAdapter("card_title")
fun bindCardTitle(cardView: TitleCardView, title: String) {
    cardView.binding.title = title
}

@BindingAdapter("card_state")
fun bindCardState(cardView: TitleCardView, state: String?) {
    cardView.binding.state = state
}

@BindingAdapter("card_option")
fun bindCardOption(cardView: TitleCardView, option: () -> Unit) {
    cardView.binding.option = option
}

@BindingAdapter("card_pre_index")
fun bindCardIndex(cardView: TitleCardView, index: Int) {
    cardView.binding.apply {
        preIndex = index.toString()
        preIcon = cardView.context.getDrawable(R.drawable.img_order_background)
    }
}

@BindingAdapter("cardStateTagColor")
fun bindCardStateColor(cardView: CardView, color: Int) {
    val colorFromAttr = cardView.context.getColorFromAttr(color)
    val colorFromColorInt =
        if (colorFromAttr != 0) colorFromAttr else color
    cardView.setCardBackgroundColor(colorFromColorInt)
    cardView.radius = 10F
}

@BindingAdapter("cardTextColor")
fun bindCardTextColor(textView: TextView, color: Int) {
    val colorFromAttr = textView.context.getColorFromAttr(color)
    val colorFromColorInt =
        if (colorFromAttr != 0) colorFromAttr else color
    textView.setTextColor(colorFromColorInt)
}

@BindingAdapter("chart_title")
fun bindChartTitle(chartView: LineChartView, title: String?) {
    chartView.binding.title = title
}

@BindingAdapter("chart_option_text")
fun bindChartOptionText(chartView: LineChartView, text: String?) {
    chartView.binding.optionText = text
}

@BindingAdapter("chart_option")
fun bindChartOption(chartView: LineChartView, option: () -> Unit) {
    chartView.binding.option = option
}

@BindingAdapter("chart_close_option")
fun bindChartCloseOption(chartView: LineChartView, option: () -> Unit) {
    chartView.binding.closeOption = option
}

@BindingAdapter("linearLayoutBackgroundColor")
fun bindLinearLayoutBackgroundColor(linearLayout: LinearLayout, color: Int) {
    val colorFromAttr = linearLayout.context.getColorFromAttr(color)
    val colorFromColorInt =
        if (colorFromAttr != 0) colorFromAttr else color
    linearLayout.setBackgroundColor(colorFromColorInt)
}

@BindingAdapter("imageColor")
fun bindImageColor(imageView: ImageView, color: Int) {
    if (imageView.drawable != null) {
        imageView.apply {
            val colorFromAttr = context.getColorFromAttr(color)
            val colorFromColorInt =
                if (colorFromAttr != 0) colorFromAttr else color
            drawable.setTint(colorFromColorInt)
        }
    }
}

@BindingAdapter("textViewColor")
fun bindTextViewColor(textView: TextView, color: Int) {
    val colorFromAttr = textView.context.getColorFromAttr(color)
    val colorFromColorInt =
        if (colorFromAttr != 0) colorFromAttr else color
    textView.setTextColor(colorFromColorInt)
}

@BindingAdapter("textViewWeight")
fun bindTextViewWeight(textView: TextView, weight: Float) {
    textView.apply {
        layoutParams = LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT,
            weight
        )
    }
}

@BindingAdapter("viewVisibility")
fun bindViewVisibility(view: View, visibility: Int) {
    if (visibility > 0) {
        view.visibility = View.VISIBLE
    } else {
        view.visibility = View.GONE
    }
}

@BindingAdapter("buttonColor")
fun bindButtonColor(button: Button, color: Int) {
    val colorFromAttr = button.context.getColorFromAttr(color)
    val colorFromColorInt =
        if (colorFromAttr != 0) colorFromAttr else color
    button.apply {
        setTextColor(colorFromColorInt)
        val drawable = ContextCompat.getDrawable(
            context,
            R.drawable.ic_alert_banner_arrow
        )
        drawable?.setTint(colorFromColorInt)

        drawable?.let {
            it.setBounds(
                0, 0, it.minimumWidth,
                it.minimumHeight
            )
            setCompoundDrawables(
                null, null, it, null
            )
        }
    }
}
